package com.hytech.utilities;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

// this is a simple activity to show an info page. 
public class InfoActivity extends Activity {

	public static final String INFO_ID = "INFO_RESOURCE_NAME";
		
	@Override
	protected void onCreate(Bundle savedInstanceState) {
	
		super.onCreate(savedInstanceState);
		setContentView(R.layout.infopage);
	
		
		// Extras are provided by checkEula() below.
		Intent i = getIntent();
		
		Bundle b = i.getExtras();
		int info_resource_id = b.getInt(INFO_ID);
		
		// save the finish button.
		Button mOK = (Button) findViewById(R.id.buttonOK);
		mOK.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				finish();
			}
		});
		
		// save the info page. 
		TextView text = (TextView) findViewById(R.id.infotext);
		text.setText(loadInfoPageFromRawResource(info_resource_id));
		
	        
	}
	
	/**
	 * Read info from raw resource.
	 * @param resourceid ID of the raw resource.
	 * @return
	 */
	private String loadInfoPageFromRawResource(int resourceid) {

		// Retrieve license from resource:
		String output = "";
		Resources resources = getResources();
    		
		//Read in the license file as a big String
		BufferedReader in
		   = new BufferedReader(new InputStreamReader(
				resources.openRawResource(resourceid)));
		
		String line;
		StringBuilder sb = new StringBuilder();
		try {
			while ((line = in.readLine()) != null) { // Read line per line.
				if (TextUtils.isEmpty(line)) {
					// Empty line: Leave line break
					sb.append("\n");
				} else {
					sb.append(line);
					sb.append(" \n");
				}
			}
			output = sb.toString();
		} catch (IOException e) {
			//Should not happen.
			e.printStackTrace();
		}
		
    	
    	return output;
	}
	
	
}